<template>
  <div class="list-content">
    <el-row type="flex" justify="space-between">
      <el-form class="serach-form" :inline="true" :model="formInline" @submit.native.prevent >
        <el-form-item v-if="isMuilUnitUser">
          <el-select filterable @change="getData(1)" v-model="formInline.orgIds" placeholder="请选择业主单位">
            <el-option
              v-for="item in orgOptions"
              :key="item.organizationId"
              :label="item.shortName"
              :value="item.organizationId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input 
          v-model.trim="formInline.name" 
          placeholder="请输入点位类别名称"
          @keyup.enter.native="getList(1)"
          clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="form-search-btn" type="primary" @click="getList(1)">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="more-btns">
        <el-button v-if="isMuilUnitUser" type="primary" @click="addPointType(1)">新增</el-button>
      </div>
    </el-row>
    <el-row class="overview-wrap" :gutter="0"  type="flex" justify="center">
      <el-col :span="24">
        <div class="item-wrap">
          <p class="item-value color-4">{{ formInline.currentPageTotal }}</p>
          <p class="item-title">类别总数</p>
        </div>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="tableData"
      ref="mainTable"
      style="width: 100%">
      <el-table-column
        label="序号"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column
        v-if="isMuilUnitUser"
        prop="orgName"
        label="业主单位">
      </el-table-column>
      <el-table-column prop="picUrl" label="图片" width="100" >
        <!-- 图片的显示 -->
        <template  slot-scope="scope">            
          <img :src="scope.row.picUrl?scope.row.picUrl.split(',')[0]:defaultImg"  width="50" height="50" @click="$imgPreview" :data-imgpreview="scope.row.picUrl"/>
        </template>         
      </el-table-column>
      <el-table-column
        prop="pointTypeName"
        label="类别名称">
      </el-table-column>
      <el-table-column
        v-if="!isMuilUnitUser"
        prop="number"
        label="检查内容数">
      </el-table-column>
      <el-table-column
        v-if="isMuilUnitUser"
        prop="creatAccountName"
        label="创建人">
      </el-table-column>
      <el-table-column
        v-if="isMuilUnitUser"
        label="创建时间">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column 
        label="操作">
        <template slot-scope="scope">
          <el-button  @click="showDetail(scope.$index, scope.row)" type="text" size="small">详情</el-button>
          <span v-if="!isMuilUnitUser" class="dividing-line">|</span>
          <el-button v-if="!isMuilUnitUser" @click="addPointType(scope.$index, scope.row)" type="text" size="small">编辑</el-button>
          <span v-if="!isMuilUnitUser" class="dividing-line">|</span>
          <el-button v-if="!isMuilUnitUser" @click="delPointType(scope.$index, scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
      <!-- 展开详情 -->
      <el-table-column type="expand" width="1">
        <template slot-scope="scope">
          <div class="detail-content">
            <p><b>点位类别名称：</b>{{scope.row.pointTypeName}}</p>
            <p v-for="(item,index) in scope.row.contentList" :key="index"><b>检查内容{{(index+1)}}：</b>{{item.content}}&nbsp;&nbsp;&nbsp;&nbsp;{{util.ilinkArrHandle.getArrObjAttrToArr(item.attr,"option").join("/")}}</p>
            <p v-if="scope.row.picUrl">
              <img v-for="(item,index) in scope.row.picUrl.split(',')" :key="index" :src="item" width="100" height="100" @click="$imgPreview" :data-imgpreview="scope.row.picUrl" :data-imgpreviewindex="index"/>
            </p>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChange"
      @current-change="pageChange"
      :current-page="formInline.currentPageIndex"
      :page-sizes="[10, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="formInline.currentPageTotal">
    </el-pagination>
    <el-dialog
      :close-on-click-modal="false"
      title="点位类别维护"
      width="600px"
      top='10vh'
      v-if="dialogFalg"
      :visible.sync="dialogFalg"
      @close="closeDialog"
      v-dialogDrag>
      <point-type-maintain :editInfo="editInfo" :closeDialog="closeDialog"></point-type-maintain>
    </el-dialog>
  </div>
</template>

<script>
import PointTypeMaintain from './Dialog/PointTypeMaintain'
import util from '@/utils/util'
import { getOrgList} from '@/api/org'
import { getInspectPointTypeList,delInspectPointType} from '@/api/inspect'

export default {
  name: 'PointType',
  props:['param'],
  components: { 
    PointTypeMaintain 
  },
  data () {
    return {
      formInline: {
        name: '',
        result:'-1',
        orgIds:this.$store.state.userinfo.isMuilUnitUser ? this.$store.state.userinfo.organizationIds : this.$store.state.userinfo.org.id,
        currentPageSize: 10,
        currentPageIndex: 1,
        currentPageTotal: 0
      },
      util:util,
      isMuilUnitUser:this.$store.state.userinfo.isMuilUnitUser,
      accountName:this.$store.state.userinfo.accountName,
      defaultImg: this.GLOBAL.defaultImg,
      tableData: [],//表格数据
      orgOptions: [{organizationId: this.$store.state.userinfo.organizationIds,shortName: "全部业主单位"}],
      loading: false,
      dialogFalg: false,
      editInfo: { // 编辑对应的表单信息
        /*id:0,
        orgId:'',
        pointTypeName : '',
        picUrl:'',
        createAccount:this.$store.state.userinfo.accountName,
        updateAccount:this.$store.state.userinfo.accountName,
        contents:'',*/
      }
    }
  },
  created:function() {
   this.getList()    
    //绑定下拉框
    getOrgList({queryType: 0}).then((data)=>{
      this.orgOptions.push.apply(this.orgOptions,data.data);
    })
  },
  methods: {
    showDetail(index, row){//点位详情
      const $table = this.$refs.mainTable
      row.isExpand = !row.isExpand;//给row一个自定义展开状态
      $table.toggleRowExpansion(row)
    },
    sizeChange(val) {//页码切换
      this.formInline.currentPageIndex = 1;
      this.formInline.currentPageSize = val;
      this.getList() 
    },
    pageChange(val) {//翻页
      this.formInline.currentPageIndex = val;
      this.getList() 
    },
    getData(_pageIndex){
      this.getList(_pageIndex) 
    },
    getList(_pageIndex) {//获取列表
      if(_pageIndex){
        this.formInline.currentPageIndex = _pageIndex
      }
      this.loading =true;
      getInspectPointTypeList({
        queryType : 1,
        pageIndex: this.formInline.currentPageIndex -1,
        pageSize: this.formInline.currentPageSize,
        orgIds: this.formInline.orgIds,
        name: this.formInline.name.trim()
      }).then((result) => {
        this.formInline.currentPageTotal = result.total;
        this.tableData = result.data;
        this.loading =false;
      }) 
    },
    addPointType(index, row) {
      this.dialogFalg = true
      this.editInfo = row||{}
    },
    closeDialog(refresh) {
      this.dialogFalg = false
      if(refresh) {
        this.getList(this.editInfo.id?'':1)
      }
    },
    delPointType(index, row) {
      let that = this
      this.$confirm("删除该点位类别可能会影响现有巡检流程及巡检记录，确定删除？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        roundButton: true
      }).then(() => {
        delInspectPointType({id: row.id}).then(res => {
          if( res.status === 200) {
            if( res.data === 200011){
              this.$message({
                message: '该点位类型已被关联，不能删除！',
                type: 'warning'
              })
            }else{
              that.$message({
                type: 'success',
                message: '删除成功!'
              })
              that.getList(1)
            }
          }else{
            that.$message.error(res.info);
          }
        })
      }).catch(() => {

      })
    },
  },
}
</script>
